package com.vgemv.jsutilitysdkdemo.module.log;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.vgemv.jsutilitysdk.activity.jsloglistactivity.JsLogListActivity;
import com.vgemv.jsutilitysdk.Display.JsActivityContentDisplayManager.JsActivityContentDisplayManager;
import com.vgemv.jsutilitysdk.Email.JsEmailManager;
import com.vgemv.jsutilitysdk.Email.JsEmailSendCallback;
import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.log.JsLogManager;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuFragment.JsMenuFragment.JsMenuFragment;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVmDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;

import java.util.ArrayList;

public class LogActivity extends AppCompatActivity {


    public Handler mHandler = new Handler(Looper.getMainLooper());

    private JsLogManager mLm_main = JsLogManager.sharedInstance();
    private JsLogManager mLm_sec;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);



        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setId(View.generateViewId());
        linearLayout.setBackgroundColor(Color.parseColor("#ffaabbcc"));
        setContentView(linearLayout);

        mLm_main.initViewLogTarget(LogActivity.this);
        JsMenuFragment gajOneLevelMenuFragment = new JsMenuFragment();
        gajOneLevelMenuFragment.moveToParentView(this,linearLayout);

        gajOneLevelMenuFragment.mBasicCallback = new JsMenuViewBasicCallback() {
            @Override
            public Rect jsMenuViewSetItemOffset(JsMenuView fragment, int index) {
                return null;
            }

            @Override
            public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                return null;
            }

            @Override
            public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

            }

            @Override
            public JsMenuViewVm jsMenuViewSetVm(JsMenuView fragment) {
                return null;
            }
            @Override
            public JsMenuItemView jsMenuViewCreateItemView(JsMenuView fragment, Context context) {
                return null;
            }

            @Override
            public void jsMenuViewAfterCreateView(JsMenuView fragment) {

            }

            @Override
            public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                ArrayList<JsMenuItemVm> models = new ArrayList<JsMenuItemVm>();
                models.add(new JsMenuItemVmDefault(0,"控制台输出日志",""));
                models.add(new JsMenuItemVmDefault(1,"打印日志目录",""));
                models.add(new JsMenuItemVmDefault(2,"清空今日日志",""));
                models.add(new JsMenuItemVmDefault(3,"显示今日日志文件路径",""));
                models.add(new JsMenuItemVmDefault(4,"发送今日日志到邮箱",""));
                models.add(new JsMenuItemVmDefault(5,"显示日志列表",""));
                models.add(new JsMenuItemVmDefault(6,"文件+控制台输出日志",""));
                models.add(new JsMenuItemVmDefault(7,"ActiveViewTarget",""));
                models.add(new JsMenuItemVmDefault(8,"InactiveViewTarget",""));
                return models;
            }

            @Override
            public void jsMenuViewClickItem(JsMenuView fragmentAlpha, int i, JsMenuItemVm model) {
                switch (i) {
                    case 0:{
//                        JsLogManager.logfc_d(null,"This message only displays at console");
                        JsLogManager.sharedInstance().log(JsLogManager.JsLogLevelInfo,JsLogManager.JsLogTargetAll,"LogActivity","this is a message to all target");
                        break;
                    }
                    case 1:{
                        JsLogManager.sharedInstance().printLogFileDir();
                        break;
                    }
                    case 2: {
                        JsLogManager.sharedInstance().clearTodayFile();
                        break;
                    }
                    case 3: {
                        String filePath = JsLogManager.sharedInstance().getTodayFilePath();
                        JsLogManager.logfc_d(null,"today file path is %s",filePath);
                        break;
                    }
                    case 4: {
                        JsEmailManager.sendMail(JsLogManager.sharedInstance().getTodayFilePath(), "OprateLog", null, false, new JsEmailSendCallback() {
                            @Override
                            public void onMailSendSuccess() {
                                JsActivityContentDisplayManager.sharedInstance().displayMessage("发送成功",LogActivity.this);
                                JsLogManager.sharedInstance().clearTodayFile();
                            }

                            @Override
                            public void onMailSendFail() {
                                JsActivityContentDisplayManager.sharedInstance().displayMessage("发送失败",LogActivity.this);
                            }
                        });
                        break;
                    }
                    case 5: {
                        Intent intent = new Intent(LogActivity.this, JsLogListActivity.class);
                        startActivity(intent);
                        break;
                    }
                    case 6:{
                        JsLogManager.logfc_d(null,"This message displays at console and log file.");
                        break;
                    }
                    case 7:{
//                        ViewGroup.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,500);
                        int screenHeight = JsGeometryManager.getScreenSizeInPixel().getHeight();
                        int viewHeight = screenHeight * 3/4;
                        int screenWidth = JsGeometryManager.getScreenSizeInPixel().getWidth();
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(screenWidth,viewHeight);
                        lp.setMargins(-screenWidth,JsGeometryManager.getScreenSizeInPixel().getHeight()-viewHeight,0,0);
                        mLm_main.getViewTarget().activate(linearLayout,lp);
                        break;
                    }
                    case 8:{
                        mLm_main.getViewTarget().inactivate();
                        break;
                    }
                    default:{
                        break;
                    }
                }
            }
        };
    }
}